<?php


abstract class BaseKernelUbicacion extends BaseObject  implements Persistent {


	
	protected static $peer;

	
	protected $id;

	
	protected $tercero_id;

	
	protected $lugar;

	
	protected $telefono;

	
	protected $direccion;

	
	protected $ciudad_id;

	
	protected $aKernelTercero;

	
	protected $aKernelCiudad;

	
	protected $collKernelSucursals;

	
	private $lastKernelSucursalCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getTerceroId()
	{
		return $this->tercero_id;
	}

	
	public function getLugar()
	{
		return $this->lugar;
	}

	
	public function getTelefono()
	{
		return $this->telefono;
	}

	
	public function getDireccion()
	{
		return $this->direccion;
	}

	
	public function getCiudadId()
	{
		return $this->ciudad_id;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = KernelUbicacionPeer::ID;
		}

		return $this;
	} 
	
	public function setTerceroId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->tercero_id !== $v) {
			$this->tercero_id = $v;
			$this->modifiedColumns[] = KernelUbicacionPeer::TERCERO_ID;
		}

		if ($this->aKernelTercero !== null && $this->aKernelTercero->getId() !== $v) {
			$this->aKernelTercero = null;
		}

		return $this;
	} 
	
	public function setLugar($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->lugar !== $v) {
			$this->lugar = $v;
			$this->modifiedColumns[] = KernelUbicacionPeer::LUGAR;
		}

		return $this;
	} 
	
	public function setTelefono($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->telefono !== $v) {
			$this->telefono = $v;
			$this->modifiedColumns[] = KernelUbicacionPeer::TELEFONO;
		}

		return $this;
	} 
	
	public function setDireccion($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->direccion !== $v) {
			$this->direccion = $v;
			$this->modifiedColumns[] = KernelUbicacionPeer::DIRECCION;
		}

		return $this;
	} 
	
	public function setCiudadId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->ciudad_id !== $v) {
			$this->ciudad_id = $v;
			$this->modifiedColumns[] = KernelUbicacionPeer::CIUDAD_ID;
		}

		if ($this->aKernelCiudad !== null && $this->aKernelCiudad->getId() !== $v) {
			$this->aKernelCiudad = null;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
			$this->tercero_id = ($row[$startcol + 1] !== null) ? (int) $row[$startcol + 1] : null;
			$this->lugar = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->telefono = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
			$this->direccion = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
			$this->ciudad_id = ($row[$startcol + 5] !== null) ? (int) $row[$startcol + 5] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 6; 
		} catch (Exception $e) {
			throw new PropelException("Error populating KernelUbicacion object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aKernelTercero !== null && $this->tercero_id !== $this->aKernelTercero->getId()) {
			$this->aKernelTercero = null;
		}
	
		if ($this->aKernelCiudad !== null && $this->ciudad_id !== $this->aKernelCiudad->getId()) {
			$this->aKernelCiudad = null;
		}
	
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelUbicacionPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = KernelUbicacionPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aKernelTercero = null;
			$this->aKernelCiudad = null;
			$this->collKernelSucursals = null;
			$this->lastKernelSucursalCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelUbicacionPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
			$con->beginTransaction();
			KernelUbicacionPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(KernelUbicacionPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}

		try {
		    if($this->validate())
		    {
    			$con->beginTransaction();
    			$affectedRows = $this->doSave($con);
    			$con->commit();
    			KernelUbicacionPeer::addInstanceToPool($this);
    			return true;
			}else
			{
			     
                 return false;
            }
		} catch (PropelException $e) {
			$con->rollback();
			
			
			ErrorLog::reportarError('errors', 'error',  $e->getMessage() );
			return false;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aKernelTercero !== null) {
				if ($this->aKernelTercero->isModified() || $this->aKernelTercero->isNew()) {
					$affectedRows += $this->aKernelTercero->save($con);
				}
				$this->setKernelTercero($this->aKernelTercero);
			}

			if ($this->aKernelCiudad !== null) {
				if ($this->aKernelCiudad->isModified() || $this->aKernelCiudad->isNew()) {
					$affectedRows += $this->aKernelCiudad->save($con);
				}
				$this->setKernelCiudad($this->aKernelCiudad);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = KernelUbicacionPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += KernelUbicacionPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			if ($this->collKernelSucursals !== null) {
				foreach ($this->collKernelSucursals as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aKernelTercero !== null) {
				if (!$this->aKernelTercero->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aKernelTercero->getValidationFailures());
				}
			}

			if ($this->aKernelCiudad !== null) {
				if (!$this->aKernelCiudad->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aKernelCiudad->getValidationFailures());
				}
			}


			if (($retval = KernelUbicacionPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collKernelSucursals !== null) {
					foreach ($this->collKernelSucursals as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = KernelUbicacionPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getTerceroId();
				break;
			case 2:
				return $this->getLugar();
				break;
			case 3:
				return $this->getTelefono();
				break;
			case 4:
				return $this->getDireccion();
				break;
			case 5:
				return $this->getCiudadId();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = KernelUbicacionPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getTerceroId(),
			$keys[2] => $this->getLugar(),
			$keys[3] => $this->getTelefono(),
			$keys[4] => $this->getDireccion(),
			$keys[5] => $this->getCiudadId(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = KernelUbicacionPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setTerceroId($value);
				break;
			case 2:
				$this->setLugar($value);
				break;
			case 3:
				$this->setTelefono($value);
				break;
			case 4:
				$this->setDireccion($value);
				break;
			case 5:
				$this->setCiudadId($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = KernelUbicacionPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setTerceroId($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setLugar($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setTelefono($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setDireccion($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setCiudadId($arr[$keys[5]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(KernelUbicacionPeer::DATABASE_NAME);

		if ($this->isColumnModified(KernelUbicacionPeer::ID)) $criteria->add(KernelUbicacionPeer::ID, $this->id);
		if ($this->isColumnModified(KernelUbicacionPeer::TERCERO_ID)) $criteria->add(KernelUbicacionPeer::TERCERO_ID, $this->tercero_id);
		if ($this->isColumnModified(KernelUbicacionPeer::LUGAR)) $criteria->add(KernelUbicacionPeer::LUGAR, $this->lugar);
		if ($this->isColumnModified(KernelUbicacionPeer::TELEFONO)) $criteria->add(KernelUbicacionPeer::TELEFONO, $this->telefono);
		if ($this->isColumnModified(KernelUbicacionPeer::DIRECCION)) $criteria->add(KernelUbicacionPeer::DIRECCION, $this->direccion);
		if ($this->isColumnModified(KernelUbicacionPeer::CIUDAD_ID)) $criteria->add(KernelUbicacionPeer::CIUDAD_ID, $this->ciudad_id);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(KernelUbicacionPeer::DATABASE_NAME);

		$criteria->add(KernelUbicacionPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setTerceroId($this->tercero_id);

		$copyObj->setLugar($this->lugar);

		$copyObj->setTelefono($this->telefono);

		$copyObj->setDireccion($this->direccion);

		$copyObj->setCiudadId($this->ciudad_id);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getKernelSucursals() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addKernelSucursal($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new KernelUbicacionPeer();
		}
		return self::$peer;
	}

	
	public function setKernelTercero(KernelTercero $v = null)
	{
		if ($v === null) {
			$this->setTerceroId(NULL);
		} else {
			$this->setTerceroId($v->getId());
		}

		$this->aKernelTercero = $v;

						if ($v !== null) {
			$v->addKernelUbicacion($this);
		}

		return $this;
	}


	
	public function getKernelTercero(PropelPDO $con = null)
	{
		if ($this->aKernelTercero === null && ($this->tercero_id !== null)) {
			$this->aKernelTercero = KernelTerceroPeer::retrieveByPK($this->tercero_id, $con);
			
		}
		return $this->aKernelTercero;
	}

	
	public function setKernelCiudad(KernelCiudad $v = null)
	{
		if ($v === null) {
			$this->setCiudadId(NULL);
		} else {
			$this->setCiudadId($v->getId());
		}

		$this->aKernelCiudad = $v;

						if ($v !== null) {
			$v->addKernelUbicacion($this);
		}

		return $this;
	}


	
	public function getKernelCiudad(PropelPDO $con = null)
	{
		if ($this->aKernelCiudad === null && ($this->ciudad_id !== null)) {
			$this->aKernelCiudad = KernelCiudadPeer::retrieveByPK($this->ciudad_id, $con);
			
		}
		return $this->aKernelCiudad;
	}

	
	public function clearKernelSucursals()
	{
		$this->collKernelSucursals = null; 	}

	
	public function getKernelSucursals($criteria = null, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelUbicacionPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collKernelSucursals === null) {
			if ($this->isNew()) {
			   $this->collKernelSucursals = array();
			} else {

				$criteria->add(KernelSucursalPeer::UBICACIONID, $this->getId());

				KernelSucursalPeer::addSelectColumns($criteria);
				$this->collKernelSucursals = KernelSucursalPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(KernelSucursalPeer::UBICACIONID, $this->getId());

				KernelSucursalPeer::addSelectColumns($criteria);
				if (!isset($this->lastKernelSucursalCriteria) || !$this->lastKernelSucursalCriteria->equals($criteria)) {
					$this->collKernelSucursals = KernelSucursalPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastKernelSucursalCriteria = $criteria;
		return $this->collKernelSucursals;
	}

	
	public function countKernelSucursals(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		
		if ($criteria === null) {
			$criteria = new Criteria(KernelUbicacionPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collKernelSucursals === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(KernelSucursalPeer::UBICACIONID, $this->getId());

				$count = KernelSucursalPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(KernelSucursalPeer::UBICACIONID, $this->getId());

				if (!isset($this->lastKernelSucursalCriteria) || !$this->lastKernelSucursalCriteria->equals($criteria)) {
					$count = KernelSucursalPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collKernelSucursals);
				}
			} else {
				$count = count($this->collKernelSucursals);
			}
		}
		$this->lastKernelSucursalCriteria = $criteria;
		return $count;
	}

	
	public function addKernelSucursal(KernelSucursal $l)
	{
		if ($this->collKernelSucursals === null) {
			$this->collKernelSucursals = array();
		}
		if (!in_array($l, $this->collKernelSucursals, true)) { 			array_push($this->collKernelSucursals, $l);
			$l->setKernelUbicacion($this);
		}
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			foreach($this->collKernelSucursals as $o) {
				$o->clearAllReferences($deep);
			}
		} 
		$this->collKernelSucursals = null;
	}

} 